package MainPackage 
{
	import asunit.framework.TestCase;
  import flash.events.TimerEvent;
  import flash.utils.Timer;
  import MainPackage.events.RadioHistoryEvent;
  import vk.Api;
	
	/**
   * ...
   * @author Roman Marchenko
   */
  public class TestRadioHistory extends TestCase 
  {
    private var history: RadioHistory;
    public function TestRadioHistory(testMethod:String = null) 
    {
      super(testMethod);
    }
    
    override protected function setUp () : void
    {
      Api.setUp(new Object); // empty vars array
    }
    
    public function testRadioHistory (): void 
    {
      history = new RadioHistory;
      history.addEventListener(RadioHistoryEvent.READ_COMPLETE,
                               addAsync(onReadComplete, 10001)); 
      history.load();
    }

    private function onReadComplete (e: RadioHistoryEvent): void
    {
      // Simple
      history.clear();
      assertEquals(0, history.size());
      assertNull(history.getAt(0));
      assertNull(history.getAt(15));
      
      history.add("\"ДДТ+");
      assertEquals(1, history.size());
      assertEquals("\"ДДТ+", history.getAt(0));
      assertNull(history.getAt(1));

      history.add("Maroon'5");
      assertEquals(2, history.size());
      assertEquals("Maroon'5", history.getAt(0));
      assertEquals("\"ДДТ+", history.getAt(1));
      assertNull(history.getAt(2));
 
      // Test max
      for (var i: uint = 1; i <= RadioHistory.MAX + 2; ++i)
      {
        history.add(i.toString());
      }
      assertEquals(RadioHistory.MAX, history.size());
      assertEquals(RadioHistory.MAX + 2, history.getAt(0));
      assertEquals(RadioHistory.MAX + 1, history.getAt(1));
      assertEquals(3, history.getAt(RadioHistory.MAX - 1));
      
      history.addEventListener(RadioHistoryEvent.WRITE_COMPLETE,
                               addAsync(onWriteComplete, 10002));
    }

    private function onReadComplete2 (e: RadioHistoryEvent): void
    {
      // 2. Test persistancy
      assertEquals(RadioHistory.MAX, history.size());
      assertEquals(RadioHistory.MAX + 2, history.getAt(0));
      assertEquals(RadioHistory.MAX + 1, history.getAt(1));
      assertEquals(3, history.getAt(RadioHistory.MAX - 1));
    }

    private function onWriteComplete (e: RadioHistoryEvent): void
    {
      history = new RadioHistory;
      history.addEventListener(RadioHistoryEvent.READ_COMPLETE,
                               addAsync(onReadComplete2, 10003)); 
      history.load();
   }
  }

}